package cn.lg.soar.gateway.util;

import cn.lg.soar.common.util.IpUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.MultiValueMap;

/**
 * @author luguoxiang
 * @date 2022/4/1
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public interface RequestUtils {

    static String getQueryParams(MultiValueMap<String, String> multiValueMap, String name) {
        return multiValueMap.getFirst(name);
    }

    static String getQueryParams(ServerHttpRequest request, String name) {
        return getQueryParams(request.getQueryParams(), name);
    }

    static String getHeader(HttpHeaders headers, String name) {
        return headers.getFirst(name);
    }

    static String getHeader(ServerHttpRequest request, String name) {
        return getHeader(request.getHeaders(), name);
    }

    static String getHeaderOrQueryParams(ServerHttpRequest request, String name) {
        String header = getHeader(request.getHeaders(), name);
        if (header != null) {
            return header;
        }
        return getQueryParams(request.getQueryParams(), name);
    }

    static void addHeader(ServerHttpRequest.Builder mutate, String name, Object value) {
        if (value == null) {
            return;
        }
        mutate.header(name, value.toString());
    }

    static void removeHeader(ServerHttpRequest.Builder mutate, String name) {
        mutate.headers(httpHeaders -> httpHeaders.remove(name)).build();
    }

    static void setClientIp(ServerHttpRequest.Builder mutate, String ip) {
        addHeader(mutate, IpUtil.CLIENT_IP, ip);
    }

    static String getRemoteIp(ServerHttpRequest request){
        HttpHeaders headers = request.getHeaders();
        String ip = headers.getFirst("x-forwarded-for");
        if(ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return fmtIp(ip);
        }
        // nginx 代理服务器
        ip = headers.getFirst("X-Real-IP");
        if(ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return fmtIp(ip);
        }
        // apache http 做代理
        ip = headers.getFirst("Proxy-Client-IP");
        if(ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return fmtIp(ip);
        }
        // weblogic 插件
        ip = headers.getFirst("WL-Proxy-Client-IP");
        if(ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return fmtIp(ip);
        }
        // 有些代理服务器
        ip = headers.getFirst("HTTP_CLIENT_IP");
        if(ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return fmtIp(ip);
        }

        return fmtIp(request.getRemoteAddress().getAddress().getHostAddress());
    }

    static String fmtIp(String ip){
        ip = ip.split(",")[0];
        if("localhost".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)){
            return "127.0.0.1";
        }
        return ip;
    }
}
